﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NangLuongTieuDiemDTO.DTO;

namespace NangLuongTieuDiemGUI.NangLuongTieuDiemUserControl
{
    public partial class ucSoGioNang : UserControl
    {
        #region "[khai báo biến toàn cục]"
        public DelegateShowItem closeItem = null;

        private int m_IDSoGioNang = -1;
        #endregion

        #region "[khởi tạo mặc định]"
        public ucSoGioNang()
        {
            InitializeComponent();
        }
        #endregion

        #region "[thoát]"
        private void btnThoat_Click(object sender, EventArgs e)
        {
            try {
                if (closeItem != null)
                    closeItem(false);
            }
            catch { }
        }
        #endregion

        #region "[thêm số giờ năng]"
        private void btnThem_Click(object sender, EventArgs e)
        {
            try {
                if (txttenkhuvuc.Text.Trim() == string.Empty)
                {
                    ClassUtils.Utils.MassageBoxErrorOtInformation("Tên khu vực không được để trống", "Thông Báo", true);
                    return;
                }
                if (txtsogionang.Text.Trim() == string.Empty)
                {
                    ClassUtils.Utils.MassageBoxErrorOtInformation("Số giờ nắng không được bỏ trống", "Thông Báo", true);
                    return;
                }
                SoGioNangDTO Item = new SoGioNangDTO();
                Item.TenKhuVuc = txttenkhuvuc.Text.Trim();
                Item.SoGioNang = double.Parse(txtsogionang.Text.Trim());
                Item.Xoa = false;
                Item.GhiChu = txtGhiChu.Text.Trim();

                int ID = NangLuongTieuDiemBUS.NangLuongTieuDiem_Management.SoGioNang_Insert(Item);
                if (ID > 0)
                {
                    LoadDataGridview();
                    ClearControlInput();
                }
            }
            catch { }
        }
        #endregion

        #region "[xóa 1 dòng trên gridview]"
        private void btnXoa_Click(object sender, EventArgs e)
        {
            try {
                if (m_IDSoGioNang > 0)
                { 
                    SoGioNangDTO Item  = NangLuongTieuDiemBUS.NangLuongTieuDiem_Management.SoGioNang_SelectByPrimaryKey(m_IDSoGioNang);
                    Item.Xoa=true;
                    bool isSuccess = NangLuongTieuDiemBUS.NangLuongTieuDiem_Management.SoGioNang_Update(Item);
                    if (isSuccess)
                    {
                        LoadDataGridview();
                        m_IDSoGioNang = -1;
                        EnableControl(true);
                        ClearControlInput();
                    }
                }
            }
            catch { }
        }
        #endregion

        #region "[cập nhật 1 dòng trên gridview]"
        private void btnSua_Click(object sender, EventArgs e)
        {
            try {
                if (m_IDSoGioNang > 0)
                {
                    if (txttenkhuvuc.Text.Trim() == string.Empty)
                    {
                        ClassUtils.Utils.MassageBoxErrorOtInformation("Tên khu vực không được để trống", "Thông Báo", true);
                        return;
                    }
                    if (txtsogionang.Text.Trim() == string.Empty)
                    {
                        ClassUtils.Utils.MassageBoxErrorOtInformation("Số giờ nắng không được bỏ trống", "Thông Báo", true);
                        return;
                    }
                    SoGioNangDTO Item = NangLuongTieuDiemBUS.NangLuongTieuDiem_Management.SoGioNang_SelectByPrimaryKey(m_IDSoGioNang);
                    if (Item != null)
                    {
                        Item.TenKhuVuc = txttenkhuvuc.Text;
                        Item.SoGioNang = double.Parse(txtsogionang.Text.Trim());
                        bool isSuccess = NangLuongTieuDiemBUS.NangLuongTieuDiem_Management.SoGioNang_Update(Item);
                        if (isSuccess)
                        {
                            LoadDataGridview();
                            ClearControlInput();
                            m_IDSoGioNang = -1;
                            EnableControl(true);
                        }

                    }
                }
            }
            catch { }
        }
        #endregion

        #region "[Hủy thao tác]"
        private void btnHuy_Click(object sender, EventArgs e)
        {
            try {
                m_IDSoGioNang = -1;
                EnableControl(true);
                ClearControlInput();
            }
            catch { }
        }
        #endregion

        #region "[chọn 1 dòng trên gridview]"
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try {
                SoGioNangDTO Item = (SoGioNangDTO)gridView1.GetRow(e.RowHandle);
                if (Item != null)
                {
                    m_IDSoGioNang = Item.ID;
                    txttenkhuvuc.Text = Item.TenKhuVuc;
                    txtsogionang.Text = Item.SoGioNang.ToString();
                    EnableControl(false);
                }
            }
            catch { }
        }
        #endregion

        #region "[Enable control button]"
        /// <summary>
        /// Enable control button
        /// </summary>
        /// <param name="_isFlag"></param>
        private void EnableControl(bool _isFlag)
        {
            try {
                btnThem.Enabled = _isFlag;
                btnXoa.Enabled = btnSua.Enabled = !_isFlag;
            }
            catch { }
        }
        #endregion

        #region "[đánh số thứ tự trong gridview]"
        private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            try {
                if (e.Column.Name == "colSTT")
                    e.DisplayText = (e.RowHandle + 1).ToString();
            }
            catch { }
        }
        #endregion

        #region "[load]"
        private void ucSoGioNang_Load(object sender, EventArgs e)
        {
            try
            {
                //lấy danh sách các số giờ nắng theo từng khu vực
                LoadDataGridview();
                EnableControl(true);
            }
            catch { }
        }
        #endregion

        #region "[Load datagridview]"
        /// <summary>
        /// Load datagridview
        /// </summary>
        private void LoadDataGridview()
        {
            try
            {
                List<SoGioNangDTO> list = NangLuongTieuDiemBUS.NangLuongTieuDiem_Management.SoGioNang_GetAll().Where(p => p.Xoa == false).ToList();
                if (list.Count > 0)
                {
                    dgvSoGioNang.DataSource = list;
                    dgvSoGioNang.RefreshDataSource();
                }
            }
            catch { }
        }
        #endregion

        #region "[không cho người dùng nhập chữ]"
        private void txtsogionang_KeyPress(object sender, KeyPressEventArgs e)
        {
            try {
                if (!char.IsControl(e.KeyChar) && !char.IsNumber(e.KeyChar) &&e.KeyChar!='.' )
                    e.Handled = true;
            }
            catch { }
        }
        #endregion

        #region "[clear control]"
        private void ClearControlInput()
        {
            try {
                txttenkhuvuc.Text = txtGhiChu.Text = txtsogionang.Text = string.Empty;
            }
            catch { }
        }
        #endregion
    }
}
